//
//  BaseScrollViewController.m
//  RedManStock
//
//  Created by lanou3g on 15/7/19.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "BaseScrollViewController.h"
#import "HeadControlView.h"
#import "UIDefine.h"
#import "BaseTableViewController.h"
@interface BaseScrollViewController ()<UIScrollViewDelegate>

@property (nonatomic,retain) HeadControlView *smallHCView;

@property (nonatomic,retain) UIScrollView *bigScrollView;

@property (nonatomic,retain) NSArray *titleArray;
@property (nonatomic,retain) NSArray *controllerArray;

@end

@implementation BaseScrollViewController

- (instancetype)initWithTitles:(NSArray *)titleArray
                   controllers:(NSArray *)controllerArray{
    
    if (self=[super init]) {
        _num=titleArray.count;
        
        self.titleArray=titleArray;
        self.controllerArray=controllerArray;
    } 
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets=NO;
    
//布局View层
    [self setupSelfView];
    
//添加默认控制器,并向BigScrollView上添加控制器的根视图
    UIViewController *mainBTVC=self.childViewControllers[0];
    mainBTVC.view.frame=self.bigScrollView.bounds;
    [self.bigScrollView addSubview:mainBTVC.view];
    
    
}


- (void)setupSelfView{
    
    self.smallHCView=[[HeadControlView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 30) titleArray:_titleArray];
    
    for (UIButton *button in self.smallHCView.buttonArray) {
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    self.bigScrollView=[UIScrollView scrollViewWithFrame:CGRectMake(0, 94, self.view.bounds.size.width, self.view.bounds.size.height-64-30) contentSizeNum:_num];
    
    [self.view addSubview:self.bigScrollView];
    [self.view addSubview:self.smallHCView];
    
    //设置代理
    self.bigScrollView.delegate=self;
    
    //添加子控制器
    if (_controllerArray.count!=0) {
        
        for (int i=0; i<_controllerArray.count; i++) {
            [self addChildViewController:_controllerArray[i]];
        }
    }
    
}

//处理HeadControlView中的button点击事件
- (void)buttonClick:(UIButton *)button{
    
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    NSInteger index=button.tag-1000;
    
    CGFloat offsetX=index*self.bigScrollView.frame.size.width;
    CGFloat offsetY=0;
//    [self.bigScrollView setContentOffset:CGPointMake(offsetX, offsetY) animated:YES];
    
    self.bigScrollView.contentOffset=CGPointMake(offsetX, offsetY);
//    self.smallHCView.nowLocation=index;
    
    [self togetherFunction:index];
}

//bigScrollView 点击button的代码导致; 该协议方法是在上面调用了setContentOffset或者scrollRectToVisible方法，并设置了Animation=YES之后才会调用的方法!!!!!
/*
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    NSInteger index=scrollView.contentOffset.x/scrollView.frame.size.width;
//    self.smallHCView.nowLocation=index;
    
    //下方是对子控制器的操作
    
    BaseTableViewController *childBTVC=self.childViewControllers[index];
    if (childBTVC.view.superview) {
        return;
    }
    childBTVC.view.frame=scrollView.bounds;
    [self.bigScrollView addSubview:childBTVC.view];
    
}
*/

//bigScrollView 手势滚动导致
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
//    [self scrollViewDidEndScrollingAnimation:scrollView];
    
    NSInteger index=scrollView.contentOffset.x/scrollView.frame.size.width;
    
    [self togetherFunction:index];
}

//无论是button切换，还是手势滑动，这是他们最后的公有方法
- (void)togetherFunction:(NSInteger)index{
    
    self.smallHCView.nowLocation=index;
    
    //下方是对子控制器的操作
    
    BaseTableViewController *childBTVC=self.childViewControllers[index];
    if (childBTVC.view.superview) {
        return;
    }
    childBTVC.view.frame=self.bigScrollView.bounds;
    
    [self.bigScrollView addSubview:childBTVC.view];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
